﻿'' GoogleCode - http://code.google.com/p/outlook-4-you/
'' GNU GPL v3 Licencing

Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class AES
    ' CHANGE THESE KEYS - Use Generate AES to get some of your own, use them and don't publish them to the web
    Private aesKEY As Byte() = {239, 91, 205, 16, 146, 189, 253, 24, 113, 4, 72, 196, 134, 78, 118, 152, 196, 140, 132, 53, 234, 75, 107, 240, 119, 76, 113, 192, 56, 44, 85, 121}
    Private aesIV As Byte() = {249, 100, 62, 206, 131, 147, 214, 51, 1, 22, 215, 73, 32, 24, 246, 79}

    Public Shared Sub Generate_AES(ByRef key As Byte(), ByRef IV As Byte())
        Using myAes As New AesCryptoServiceProvider()
            key = myAes.Key
            IV = myAes.IV
        End Using
    End Sub ' Use to generate Key and IV

    Public Function Encrypt(ByVal plainText As String) As String
        ' Check arguments.
        If plainText Is Nothing OrElse plainText.Length <= 0 Then
            Throw New ArgumentNullException("plainText")
        End If

        Dim encrypted() As Byte
        ' Create an AesCryptoServiceProvider object
        ' with the specified key and IV.
        Using aesAlg As New AesCryptoServiceProvider()

            aesAlg.Key = aesKEY
            aesAlg.IV = aesIV

            ' Create a decrytor to perform the stream transform.
            Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for encryption.
            Dim msEncrypt As New MemoryStream()
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)
                    'Write all data to the stream.
                    swEncrypt.Write(plainText)
                End Using
                encrypted = msEncrypt.ToArray()

            End Using
        End Using

        ' Return the encrypted bytes from the memory stream.
        Return Convert.ToBase64String(encrypted)

    End Function ' Encrypt plain text to byte

    Public Function Decrypt(ByVal cipherText As String) As String

        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
            Throw New ArgumentNullException("cipherText")
        End If

        Dim cipher As Byte() = Convert.FromBase64String(cipherText)
        Dim plainText As String = ""
        Using aesAlg As New AesCryptoServiceProvider()
            aesAlg.Key = aesKEY
            aesAlg.IV = aesIV
            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
            Using msDecrypt As New MemoryStream(cipher)
                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
                    Using srDecrypt As New StreamReader(csDecrypt)
                        plainText = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        End Using

        Return plainText
    End Function ' Decrypt byte to plain text
End Class

